package hibernate;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pojo.Orderdetail;
import pojo.Orders;
import dao.OrderDetailDAO;

@Repository("OrderDetailDAO")
public class HibernateOrderDetailDAO implements OrderDetailDAO {

		private SessionFactory sessionFactory;
		
		@Autowired
		public void setSessionFactory(SessionFactory sessionFactory) {
			this.sessionFactory = sessionFactory;
		}

		@Transactional
		public boolean addOrderDetail(Orderdetail orderdetail) {
			Session session = sessionFactory.getCurrentSession();
	        try
	        {
	            session.save(orderdetail);
	            return true;
	        }
	        catch(HibernateException e)
	        {
	            System.err.print(e); 
	        }
	        return false;
		}

		@Transactional
		public List<Orderdetail> getListOrderDetailByOrderid(String orderid) {
			Session session = sessionFactory.getCurrentSession();
	        String hql =String.format("from Orderdetail o where o.orders.orderid = %s",orderid);
	        Query query = session.createQuery(hql);
	        List<Orderdetail> listOrderDetail = query.list();
	        return listOrderDetail;
		}
}
